using UnityEngine;
using System.Collections;

public class FadeTop : MonoBehaviour
{

    public Material Faded;

    public Material Original;

    //private Color FadeColor = new Color(1, 1, 1, 0.75f);
    //private Shader FadeShader;
    //private Color CompleteColor = new Color(1, 1, 1, 1);
    //private Shader CompleteShader;
    private int CurrentLevel;
    
    private const string BLOCK_TAG = "Block";

    private bool disableFadeTop = false;

    public int radius = 5;

    public void ChangeRadius(int radius)
    {
        if (radius == 0)
            this.DisableFade();
        else
            this.EnableFade();
        radius += 3;
        this.radius = radius;
        this.GetComponent<SphereCollider>().radius = radius;

    }

    // public GameObject Target;
    // Use this for initialization
    void Start()
    {
        this.CurrentLevel = GetLevel(this.transform);
        //this.CompleteShader = Shader.Find("Bumped Specular");
        //this.FadeShader = Shader.Find("Transparent/Bumped Diffuse");
     //   StartCoroutine(this.SlowUpdate());
    }

    // Update is called once per frame
    void Update()
    {
        SlowUpdate();
    }

    private void SlowUpdate()
    {
        //while (true)
        //{
          //  yield return new WaitForSeconds(1);

            int level = GetLevel(this.transform);

            if (level != CurrentLevel)
            {
                CurrentLevel = level;
                foreach (var block in Physics.OverlapSphere(this.transform.position, radius))
                {
                    this.UpdateBlock(block);
                }
            }
        //}
    }

    private int GetLevel(Transform target)
    {
        return Mathf.FloorToInt(target.position.y);
    }

    private void OnTriggerEnter(Collider block)
    {
        this.UpdateBlock(block);
    }

    private void OnTriggerExit(Collider block)
    {
        this.UnFade(block);
    }

    private void UpdateBlock(Collider block)
    {
        if (block.tag == BLOCK_TAG)
        {
            if (GetLevel(block.GetComponent<Transform>()) > CurrentLevel && !this.disableFadeTop)
            {
                block.GetComponent<Renderer>().material = this.Faded;
                //block.GetComponent<Renderer>().material.color = FadeColor;
                //block.GetComponent<Renderer>().material.shader = FadeShader;
            }
            else
            {
                block.GetComponent<Renderer>().material = this.Original;
                //block.GetComponent<Renderer>().material.color = CompleteColor;
                //block.GetComponent<Renderer>().material.shader = CompleteShader;
            }
        }

    }

    private void UnFade(Collider block)
    {
        if (block.tag == BLOCK_TAG)
        {
            block.GetComponent<Renderer>().material = this.Original;
            //block.GetComponent<Renderer>().material.color = CompleteColor;
            //block.GetComponent<Renderer>().material.shader = CompleteShader;
        }
    }

    public void DisableFade()
    {
        this.enabled = false;
        this.disableFadeTop = true;
        foreach (var block in Physics.OverlapSphere(this.transform.position, radius))
        {
            this.UpdateBlock(block);
        }
    }

    public void EnableFade()
    {
        this.enabled = true;
        this.disableFadeTop = false;
        foreach (var block in Physics.OverlapSphere(this.transform.position, radius))
        {
            this.UpdateBlock(block);
        }
    }



}
